
// 方案二
int* spiralOrder(int** matrix, int matrixSize, int* matrixColSize, int* returnSize) {
    int startRow = 0, endRow = matrixSize - 1;
    int startCol = 0, endCol = *(matrixColSize) - 1;
    *returnSize = matrixSize * (*(matrixColSize));
    int* res = (int*) malloc(sizeof(int) * (*returnSize));
    memset(res, 0, sizeof(int) * (*returnSize));
    int index = 0;
    while (startRow <= endRow && startCol <= endCol) {
        // top 行
        for (int col = startCol; col <= endCol; col++) {
            *(res + index) = *(*(matrix + startRow) + col);
            index++;
        }
        // right 列
        for (int row = startRow + 1; row <= endRow; row++) {
            *(res + index) = *(*(matrix + row) + endCol);
            index++;
        }
        if (startRow < endRow && startCol < endCol) {
            // bottom 行
            for (int col = endCol - 1; col > startCol; col--) {
                *(res + index) = *(*(matrix + endRow) + col);
                index++;
            }
            // left 列
            for (int row = endRow; row > startRow; row--) {
                *(res + index) = *(*(matrix + row) + startCol);
                index++;
            }
        }
        startRow++;
        endRow--;
        startCol++;
        endCol--;
    }
    return res;
}

// 方案一
int* spiralOrder1(int** matrix, int matrixSize, int* matrixColSize, int* returnSize) {
     int dirs[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
    int m = matrixSize;
    int n = *matrixColSize;
    int row = 0, col = 0;
    int di = 0;
    *returnSize = m * n;
    int* res = (int*) malloc(sizeof(int) * (*returnSize));
    memset(res, 0, sizeof(int) * (*returnSize));
    bool seen[m][n];
    memset(seen, false, sizeof(seen));

    for (int i = 0; i < m * n; i++) {
        *(res + i) = *(*(matrix + row) + col);
        seen[row][col] = true;

        int nextRow = row + dirs[di][0];
        int nextCol = col + dirs[di][1];

        if (nextRow < 0 || nextRow >= m || nextCol < 0 || nextCol >= n || seen[nextRow][nextCol]) {
            di = (di + 1) % 4;
            nextRow = row + dirs[di][0];
            nextCol = col + dirs[di][1];
        }

        row = nextRow;
        col = nextCol;
    }

    return res;
}